<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="com.ignou.mca.beans.UserVO"%>
<%@page import="com.ignou.mca.datasource.DatabaseOperations"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.ignou.mca.beans.QuestionsBean"%>
<%@page import="java.util.Iterator"%><html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="theme/style.css"
	media="screen" />
<title>OES - Displaying questions</title>
<script type="text/javascript">
function updateQuestion(questionId)
{
	window.location.href = './UpdateQuestion.jsp?question='+questionId;
}

function deleteQuestion(questionId)
{
	var altTxt = "You are trying to delete this Question.\n Click 'OK' to continue to delete. \n Click 'Cancel' to cancel the submission. '";
	if (confirm(altTxt))
	{
		window.location.href = 'DeleteQuestion.jsp?question='+questionId; 
	}
	else
	{
	}
}

function addQuestion(subjectId)
{
	window.location.href = './AddQuestion.jsp?subject='+subjectId;
}
</script>
</head>
<body>
	<div id="MainWrapper">
		<jsp:include page="header.jsp" /> 
		<jsp:include page="LeftContainer.jsp" />
		<div id="MainContent">
		<h2>Questions</h2>
		<%
			UserVO uvo = (UserVO) session.getAttribute("UserVO");
			if(uvo==null)
			{
				response.sendRedirect("./Login.jsp");
			}
			else
			{
				String sub = request.getParameter("subject");
				DatabaseOperations dbOperations = new DatabaseOperations();
				int subjectID = Integer.parseInt(sub);
				ArrayList<QuestionsBean> ques = dbOperations.getQuestions(subjectID);
				session.setAttribute("Ques",ques);
				%>
				<table>
				
				<%

				if(ques.size()>0)
				{
					Iterator<QuestionsBean> itr = ques.iterator();
					int quesNo = 1;
					while(itr.hasNext()) 
					{
						QuestionsBean qb = (QuestionsBean)itr.next();
						%>
						<tr><td>Question:<%=quesNo %></td><td><b><%=qb.getQuestion() %></b></td><td>
						<a href="javascript: updateQuestion(<%=qb.getQuestionId() %>)">Update</a><br>
						<a href="javascript: deleteQuestion(<%=qb.getQuestionId() %>)">Delete</a><br>
						</td></tr>
						<%	
							ArrayList<String> st = qb.getOptions();
							Iterator it = st.iterator();
							while(it.hasNext()) 
							{
								String option = (String) it.next();
								out.println("<tr><td></td><td><input type='radio' name='option"+quesNo+"' value='" + option+"'>" + option+"</input></td></tr>");
							}
						%>
						<tr><td></td><td></td></tr>
						<%
						quesNo++;
					}
				}else
				{
					
				}
				%>
				</table>
				<center>Click <a href="javascript: addQuestion(<%=subjectID %>)">here</a> to add more questions.</center>
				<%
			}
		%>
		
	
		</div>
		<jsp:include page="footer.jsp" />
	</div>
</body>
</html>